{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Gini index calculation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import division\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 2, 1]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "elements = ['A', 'A', 'A', 'C', 'B', 'C']\n",
    "def counts(elements):\n",
    "    classes = {}\n",
    "    for element in elements:\n",
    "        if element in classes:\n",
    "            classes[element] += 1\n",
    "        else:\n",
    "            classes[element] = 1\n",
    "    return [classes[e] for e in classes]\n",
    "counts(elements)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6111111111111112"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def gini(elements):\n",
    "    cts = counts(elements)\n",
    "    n = sum(cts)\n",
    "    return 1 - sum([p_i**2/n**2 for p_i in cts])\n",
    "gini(elements)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.4591479170272448"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def entropy(elements):\n",
    "    if len(elements)==0:\n",
    "        return 0\n",
    "    cts = counts(elements)\n",
    "    n = sum(cts)\n",
    "    props = 1/n*np.array(cts)\n",
    "    return -np.dot(np.log2(props), props)\n",
    "entropy(elements)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "************\n",
      "[] ['A', 'A', 'A', 'C', 'B', 'C']\n",
      "Weighted Gini: 0.6111111111111112\n",
      "Weighted entropy: 1.4591479170272446\n",
      "************\n",
      "['A'] ['A', 'A', 'C', 'B', 'C']\n",
      "Weighted Gini: 0.5333333333333333\n",
      "Weighted entropy: 1.268273412406135\n",
      "************\n",
      "['A', 'A'] ['A', 'C', 'B', 'C']\n",
      "Weighted Gini: 0.41666666666666663\n",
      "Weighted entropy: 1.0\n",
      "************\n",
      "['A', 'A', 'A'] ['C', 'B', 'C']\n",
      "Weighted Gini: 0.2222222222222222\n",
      "Weighted entropy: 0.4591479170272448\n",
      "************\n",
      "['A', 'A', 'A', 'C'] ['B', 'C']\n",
      "Weighted Gini: 0.41666666666666663\n",
      "Weighted entropy: 0.8741854163060886\n",
      "************\n",
      "['A', 'A', 'A', 'C', 'B'] ['C']\n",
      "Weighted Gini: 0.4666666666666667\n",
      "Weighted entropy: 1.1424588287122237\n"
     ]
    }
   ],
   "source": [
    "for i in range(len(elements)):\n",
    "    print(\"************\")\n",
    "    left = elements[:i]\n",
    "    right = elements[i:]\n",
    "    print(left, right)\n",
    "    weighted_gini = 1/len(elements)*(gini(left)*len(left) + gini(right)*len(right))\n",
    "    print(\"Weighted Gini:\", weighted_gini)\n",
    "    weighted_entropy = 1/len(elements)*(entropy(left)*len(left) + entropy(right)*len(right))\n",
    "    print(\"Weighted entropy:\", weighted_entropy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
